<?php
require_once 'Junction/Utils/Xml/Parser.php';
require_once 'Junction/Mapping.php';
require_once 'Junction/Mapping/Table.php';
require_once 'Junction/Mapping/References.php';
require_once 'Junction/Mapping/DirectReference.php';

class Junction_Builder {

	/**
	 * @param String $mappingFile
	 * @return Junction_Mapping
	 */
	public static function buildMapping($mappingFile) {
		try {
			$result = Junction_Utils_Xml_Parser::parseFile($mappingFile);
			$classes = $result->get('class');
			$class = $classes[0];
			// build table
			$table = new Junction_Mapping_Table($class->tablename);
			$table->addKey($class->id->name, $class->id->column);
			foreach ($class->properties as $property) {
				$table->addColumn($property->name, $property->column);
			}
			// build references
			$refs = new Junction_Mapping_References();
			foreach ($class->directReferences as $reference) {
				$ref = new Junction_Mapping_DirectReference($reference->classname);
				$refs->addReference($reference->name, $ref);
			}

			return new Junction_Mapping($class->classname, $table, $refs);
		} catch (Junction_Utils_Xml_Exception $e) {
			throw new Exception($e->getMessage()); // TODO clean up exception
		}
	}
}
?>